﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace XOS_VHD_FAT32
{
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1, Size = 512)]
     struct BootSect
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
        private byte[] Reserved1;								// jmp xxx + nop
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        private char[] _OEMName;								// OEM data (unrelevant)
        public string OEMName
        {
            get { return new string(_OEMName); }
        }
        public short BytesPerSector;							    // bytes per sector
        public byte SectorsPerCluster;								// sector per cluster 1..128
        public short ReservedSectorsCount;								// reserved sectors
        public byte FATCount;								// FAT copies count
        public short RootEntryCount;								// -- unrelevant (FAT12/16 only)
        public short TotalSectors16;								// -- unrelevant (FAT12/16 only)
        public byte Media;								// -- unrelevant (type of media)
        public short FATSize16;								// -- unrelevant (FAT12/16 only)
        public short SectorsPerTrack;								// -- unrelevant (sector per track// not used)
        public short NumberOfHeads;								// -- unrelevant (head count// not used)
        public int HiddenSectors;								// -- unrelevant (hidden sectors// not used)
        public int TotalSectors32;								// -- unrelevant (disk size// not used)
        // ----------------------------------------------------------------------------------------------
        // Extended BPB (FAT32)
        // ----------------------------------------------------------------------------------------------
        public int FATSize32;								// size of FAT area (sectors)
        public short ExtFlags;								// If bit 7 is clear then all FAT's are updated 
        // other wise bits 0-3 give the current active FAT, 
        // all other bits are reserved
        public short FSVersion;								// High byte is major revision number, low byte is 
        // minor revision number, currently both are 0.
        public int RootCluster;								// Root directory starting cluster
        public short FSInfo;								// File system information sector
        public short BkBootSector;								// If non-zero this gives the sector which holds a 
        // copy of the boot record, usually 6.
        // this area is reserved, so let's use it as storage for our data
        //bpbReserved1:													// 12 byte of 'free' space
        public int LbaStart;							// <- here we will store lba of 
        //	 the beginning of the partition
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        private byte[] Reserved2;							// ** unallocated area
        public byte DriveNumber;							// <- here we will save the drive 
        //    number, got from BIOS
        private byte Reserved3;							// ** unallocated area
        public byte BootSignature;							// FAT32 boot signature (0x28 or 0x29)
        public int VolumeId;							// Unique volume ID

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 11)]
        private char[] _volumeLabel;							// Volume label (UPPERCASE ?)
        public string VolumeLabel
        {
            get { return new string(_volumeLabel); }
        }

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        private char[] _fileSystemType;							// FS idintifier. Constant value

        public string FileSystemType
        {
            get { return new string(_fileSystemType); }
        }

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 420)]
        private byte[] BootCode;							// BootCode

        public UInt16 Magic;                             // 0xAA55
    }
}
